package com.barry.utils

import java.lang.reflect.Field

import com.barry.bean.ColumnProperty


/**
  * 类功能描述：Scala注解工具类
  *
  * @author WangXueXing create at 18-10-30 下午3:35
  * @version 1.0.0
  */
object AnnotationUtil {
  /**
    * 根据注解获取值列表
    */
  def getValueWithHead(valList: List[_]): List[String] ={
    var fields: Array[Field] = Array.empty
    val dataList: List[String] = valList.zipWithIndex.map{ x=>
      if(x._2 == 0){
        fields = x._1.getClass.getDeclaredFields.sortBy(_.getAnnotations.apply(0).asInstanceOf[ColumnProperty].index())
      }

      fields.map{f=>
         f.setAccessible(true)
         val value = f.get(x._1)
         if(value == null){
            ""
         } else {
           value.toString
         }
      }.mkString(",")
    }
    fields.map(_.getAnnotation(classOf[ColumnProperty]).name()).mkString(",")+:dataList
  }

}
